<?php

/**
 * 等级分数设定
 * 
 * @version 1.0
 * @date 2013-12-14
 */

class DefaultController extends FController {

    public $user_school_list;
    public $user_subject_list;
    public $user_role_list;
    public $exam_id;
    public function accessRules() {
        $config = array(
            'index' => 1
        );
        return ZRole::auth("grading", $config);
    }

    public function __common() {
        parent::init();
        if (empty($this->cLoginUser)) {
            $this->redirect($this->createUrl('/site/login'));
        } else {
            $userLoginUser = $this->cLoginUser;
            $this->user_role_list = isset($userLoginUser['cuser']['role_id']) ? $userLoginUser['cuser']['role_id'] : '';
            $this->user_school_list = isset($userLoginUser['cuser']['school']['school_id']) ? $userLoginUser['cuser']['school']['school_id'] : '';
            $this->user_subject_list = isset($userLoginUser['cuser']['school']['subject_id']) ? $userLoginUser['cuser']['school']['subject_id'] : '';
            $zRow = new ZRole();
            //得到学校对应的考试
            $exam_list = $zRow->_getSchoolExam($this->user_school_list);
            if (!empty($exam_list)) {
                $this->exam_id = implode(',', $exam_list);
            }
        }

        $this->commonScript();
        Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
    }

    public function actionIndex() {
        $this->__common();
        $this->title = '(高中)选修课等级分数设置';
		
		#  取考试项目  #
        $zExam = new zExam();
        $info['exam'] = $zExam->_getExamObject($this->exam_id,$this->user_role_list);
		#  取科目  #
        $examData = new ExamData();
        if ($this->user_role_list != 1) {//科目权限设定
            $info['subject'] = $examData->_getSubjectList($this->user_subject_list);
        } else {
		$info['subject'] = $examData->_getSubject();
        }
		
		$s['exam_id'] = Yii::app()->request->getParam('exam_id');
		$s['subject_id'] = Yii::app()->request->getParam('subject_id');
		$s = ZTask::dis_input($s);
		$where = ' AND "is_del" = 0';
		$order = ' ORDER BY "create_time" DESC';
		
		if(!empty($s['exam_id'])){
			$where.= ' AND "exam_id" = \''.$s['exam_id'].'\'';
		}else{
                    //权限判断
                    if ($this->user_role_list != 1 && !empty($this->exam_id)) {
                        $where = ' AND "exam_id" IN (' . $this->exam_id . ')';
                    }  
                }
		if(!empty($s['subject_id'])){
			$where.= ' AND "subject_id" = \''.$s['subject_id'].'\'';
        }else{
            if ($this->user_role_list != 1 && !empty($this->user_subject_list)) {
                $where .= " AND \"subject_id\" IN (".$this->user_subject_list.") ";
		}
        }
		
		
		$select = 'SELECT * FROM "{{point_grading}}" A';
		$sql = " WHERE 1=1  ".$where;

		$selectSql = $select.$sql.$order;
		$countSql = 'SELECT COUNT(*) FROM "{{point_grading}}" A '.$sql;
		$data = ZCommon::getDaoPageList($selectSql, $countSql, 8);
		
        if(!empty($data['listDatas'])){
			$temp = $array = array();
			
			foreach($data['listDatas'] as $key=>$value){
				#  取考试项目  #
				if(!empty($array['exam_data'][$value['exam_id']])){
					$temp['exam_data'] = $array['exam_data'][$value['exam_id']];
				}else{
					$array['exam_data'][$value['exam_id']] = $temp['exam_data'] = $examData->_getExam($value['exam_id']);
				}
				
				#  取科目  #
				if(!empty($array['subject_data'][$value['subject_id']])){
					$temp['subject_data'] = $array['subject_data'][$value['subject_id']];
				}else{
					$array['subject_data'][$value['subject_id']] = $temp['subject_data'] = $examData->_getSubject($value['subject_id']);
				}
				
				#  取等级  #
				if(!empty($array['grading_data'][$value['grading_id']])){
					$temp['grading_data'] = $array['grading_data'][$value['grading_id']];
				}else{
					$array['grading_data'][$value['grading_id']] = $temp['grading_data'] = $examData->_getGrading($value['grading_id']);
				}
				
				$data['listDatas'][$key] = array_merge($data['listDatas'][$key],$temp);
			}
		}
	
		$pageUrl = $this->createUrl('/grading/default/index');
		$this->render('index',array(
			'data'	=>$data['listDatas'],
			'pages' => $data['pages'],
			'pageUrl' => $pageUrl,
			'info'	  => $info,
			'search'	=> $s,
		));
    }

    public function actionUpdate(){
        
        $this->__common();
        
        $this->layout = "//layouts/main_window";
        $this->title = '等级分数编辑';
		
		$id = Yii::app()->request->getParam('id');
		$id = (!empty($_POST['info']['id'])) ? $_POST['info']['id'] : $id;

		$gradingModel = PointGrading::model()->findByPk($id);
		if(empty($gradingModel)){
			$this->redirect ( $this->createUrl ( 'index' ) );
			exit;
		}
		
		if(!empty($_POST['submit'])){
		
			$data = ZTask::dis_input($_POST['info']);
			
			$check = $gradingModel->find(' "exam_id" = '.$data['exam_id'].' AND "subject_id" = '.$data['subject_id'].' AND "grading_id" = '.$data['grading_id'].' AND "id"!='.$data['id']);
			if(!empty($check)){
				if($check->is_del == 1){
					$this->frameAjaxMessage ( null,'在回收站存在相同数据,请自行恢复!' );
					exit;
				}else{
					$this->frameAjaxMessage ( null,'存在相同数据正在使用!' );
					exit;
				}
			}
			
           	$data['update_user'] = $this->cLoginUser['cuser']['id'];
           	$data['update_time'] = time();
			$gradingModel->attributes = $data;
			
			if($gradingModel->save()){
				$this->frameAjaxMessage (  'index' );
			}else{	
				$this->frameAjaxMessage (null,'更新失败' );
			}
			exit;
		}
		
		#  取考试项目  #
        $zExam = new zExam();
        $info['exam'] = $zExam->_getExamObject($this->exam_id, $this->user_role_list);
		#  取科目  #
        $examData = new ExamData();
        if ($this->user_role_list != 1) {//科目权限设定
            $info['subject'] = $examData->_getSubjectList($this->user_subject_list);
        } else {
		$info['subject'] = $examData->_getSubject();
        }
		#  取等级  #
		$info['grading'] = $examData->_getGrading();
		
        $this->render('update',array(
			'info'  => $info,
			'data'	=> $gradingModel->attributes,
		));
    }
    
	
	public function actionAdd(){
        //$this->__common();
        $this->layout = "//layouts/main_window";
        $this->title = '等级分数规则创建';
		
		if(!empty($_POST['submit'])){
			$data = ZTask::dis_input($_POST['info']);
			$model = new PointGrading();
			
			$check = $model->find('"exam_id" = '.$data['exam_id'].' AND "subject_id" = '.$data['subject_id'].' AND "grading_id" = '.$data['grading_id']);
			if(!empty($check)){
				if($check->is_del == 1){
					$this->frameAjaxMessage ( null,'在回收站存在相同数据,请自行恢复!' );
					exit;
				}else{
					$this->frameAjaxMessage ( null,'存在相同数据正在使用!' );
					exit;
				}
			}
			
			$data['create_user'] = $this->cLoginUser['cuser']['id'];
           	$data['create_time'] = time();
           	$data['update_user'] = $this->cLoginUser['cuser']['id'];
           	$data['update_time'] = time();
			$model->attributes = $data;
			if($model->save()){
				$this->frameAjaxMessage ( 'index');
			}else{	
				$this->frameAjaxMessage ( null,'添加失败' );
			}
			exit;
		}
		
		#  取考试项目  #
        $zExam = new zExam();
        $info['exam'] = $zExam->_getExamObject($this->exam_id, $this->user_role_list);
		#  取科目  #
        $examData = new ExamData();
        if ($this->user_role_list != 1) {//科目权限设定
            $info['subject'] = $examData->_getSubjectList($this->user_subject_list);
        } else {
		$info['subject'] = $examData->_getSubject();
        }
		#  取等级  #
		$info['grading'] = $examData->_getGrading();
		
        $this->render('add',array(
			'info'	=> $info,
		));
    }
	
	/**
	*	@ 删除
	*	@ yindebin
	*	@ 2013-12-14 AM
	
    public function actionDelete() {
		$this->__common();
        $id = (int)Yii::app()->request->getparam('id');

		if(!empty($id)){
			$model = PointGrading::model()->findByPk($id);
			if(empty($model)){
				$this->redirect ( $this->createUrl ( 'index' ) );
				exit;
			}

			if ($model->model()->deleteAll('id=:id',array(':id'=>$id))) {
				$this->redirect ( $this->createUrl ( 'index' ) );
				exit;
			} else {
				$this->redirect ( $this->createUrl ( 'index' ) );
				exit;
			}
		}else{
			$this->redirect ( $this->createUrl ( 'index' ) );
		}
    }*/
	/**
	* @ 批量删除学段
	* @ yindebin
	* @ 2014-01-06 pm
	*
	*/
	public function actionDelete_all(){
		$id = Yii::app ()->request->getparam ( 'id' );
		if (! empty ( $id )) {
			$delete = 'DELETE FROM "{{point_grading}}" WHERE "id" IN ('.$id.')';
			$query = Yii::app()->db->createCommand($delete)->execute();
			echo 1;
		}else{
			echo 2;
		}
	}	
	
	/*
	 * 取考试项目下的科目
	 */
	public function actionGet_subject(){
            $this->__common();
		$id = Yii::app()->request->getparam('id');
		$subject_id = Yii::app()->request->getparam('subject_id');
		$subject_id = (!empty($subject_id)) ? $subject_id : 0;
		$str = '<option value="" >请选择</option>';
		if (! empty ( $id )) {
			$subject = array();
			
                        #  取科目  #
                        $examData = new ExamData();
                        if ($this->user_role_list != 1) {//科目权限设定
                            $subject = $examData->_getSubjectList($this->user_subject_list);
                        } else {
                            $subject = $examData->_getExamSubject($id);
                        }
			if(!empty($subject)){
				foreach ($subject as $key=>$value){
					if($subject_id == $value['subject_id']){
						$str.= "<option value='".$value['subject_id']."' selected>".$value['subject_name']."</option>";
					}else{
						$str.= "<option value='".$value['subject_id']."'>".$value['subject_name']."</option>";
					}
				}
			}
		
		}
		echo $str;
	}
	
	
	
}